package com.example.hrsystem.ui.approval.overtime;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hrsystem.R;
import com.example.hrsystem.workflow.ApprovalUtils;
import com.example.hrsystem.workflow.model.OvertimeRequest;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 加班申请列表适配器
 * 管理加班申请项的显示
 */
public class OvertimeRequestAdapter extends RecyclerView.Adapter<OvertimeRequestAdapter.OvertimeRequestViewHolder> {
    private List<OvertimeRequest> overtimeRequests;
    private OnItemClickListener listener;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    /**
     * 点击事件监听器
     */
    public interface OnItemClickListener {
        void onItemClick(OvertimeRequest overtimeRequest);
    }

    /**
     * 构造函数
     */
    public OvertimeRequestAdapter(List<OvertimeRequest> overtimeRequests, OnItemClickListener listener) {
        this.overtimeRequests = overtimeRequests;
        this.listener = listener;
    }

    @NonNull
    @Override
    public OvertimeRequestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.overtime_request_item, parent, false);
        return new OvertimeRequestViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OvertimeRequestViewHolder holder, int position) {
        OvertimeRequest overtimeRequest = overtimeRequests.get(position);
        holder.bind(overtimeRequest, listener);
    }

    @Override
    public int getItemCount() {
        return overtimeRequests.size();
    }

    /**
     * 更新加班申请列表数据
     */
    public void setOvertimeRequests(List<OvertimeRequest> overtimeRequests) {
        this.overtimeRequests = overtimeRequests;
        notifyDataSetChanged();
    }

    /**
     * 加班申请视图持有者
     */
    class OvertimeRequestViewHolder extends RecyclerView.ViewHolder {
        private TextView requestNumberTextView;
        private TextView statusTextView;
        private TextView overtimeDateTextView;
        private TextView timeRangeTextView;
        private TextView hoursTextView;
        private TextView overtimeTypeTextView;
        private TextView createTimeTextView;

        public OvertimeRequestViewHolder(@NonNull View itemView) {
            super(itemView);
            requestNumberTextView = itemView.findViewById(R.id.request_number_text_view);
            statusTextView = itemView.findViewById(R.id.status_text_view);
            overtimeDateTextView = itemView.findViewById(R.id.overtime_date_text_view);
            timeRangeTextView = itemView.findViewById(R.id.time_range_text_view);
            hoursTextView = itemView.findViewById(R.id.hours_text_view);
            overtimeTypeTextView = itemView.findViewById(R.id.overtime_type_text_view);
            createTimeTextView = itemView.findViewById(R.id.create_time_text_view);
        }

        /**
         * 绑定数据到视图
         */
        public void bind(final OvertimeRequest overtimeRequest, final OnItemClickListener listener) {
            // 请求编号 (暂时使用ID代替，因为没有requestNumber字段)
            requestNumberTextView.setText("加班申请 #" + overtimeRequest.getId());
            
            // 设置状态
            String status = overtimeRequest.getStatus();
            statusTextView.setText(ApprovalUtils.getStatusDisplayName(status));
            statusTextView.setTextColor(ApprovalUtils.getStatusColor(itemView.getContext(), status));
            
            // 设置加班日期
            overtimeDateTextView.setText(ApprovalUtils.formatDate(overtimeRequest.getOvertimeDate()));
            
            // 设置时间范围
            String timeRange = ApprovalUtils.formatTime(overtimeRequest.getStartTime()) + " - " + 
                              ApprovalUtils.formatTime(overtimeRequest.getEndTime());
            timeRangeTextView.setText(timeRange);
            
            // 设置加班时长
            hoursTextView.setText(String.format("%.1f小时", overtimeRequest.getHours()));
            
            // 设置加班类型
            overtimeTypeTextView.setText(overtimeRequest.getOvertimeType());
            
            // 设置创建时间
            createTimeTextView.setText(ApprovalUtils.formatDateTime(overtimeRequest.getCreatedAt()));
            
            // 设置点击事件
            itemView.setOnClickListener(v -> listener.onItemClick(overtimeRequest));
        }
    }
}